/*******************************************************************************
 * Copyright (c) 2007 University of Oslo.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     University of Oslo - initial API and implementation
 *******************************************************************************/

package com.cea.papyrus.diagram.sequence.sedi.edit.handlers;

import org.eclipse.gef.EditPolicy;
import org.eclipse.gef.commands.Command;
import org.eclipse.gef.requests.CreateRequest;
import org.eclipse.uml2.uml.InteractionFragment;

import com.cea.papyrus.core.utils.Debug;
import com.cea.papyrus.diagram.sequence.sedi.edit.command.AnalyseCoveredCommand;
import com.cea.papyrus.diagram.sequence.sedi.edit.command.SelectPartCommand;
import com.cea.papyrus.diagram.sequence.sedi.model.command.CoverCommand;
import com.cea.papyrus.diagram.sequence.sedi.model.command.CreateGraphicalElementCommand;
import com.cea.papyrus.diagram.sequence.sedi.model.command.CreateInteractionUseCommand;
import com.cea.papyrus.diagram.sequence.sedi.model.util.ModelUtil;
import com.cea.papyrus.diagraminterchange2.di2.GraphNode;

/**
 * Handles creation of Interaction Uses.
 * <p>
 * This handler is used from FrameLayoutPolicy.
 * </p>
 * 
 * @author frankd@ifi.uio.no
 */
public class InteractionUseCreationHandler extends InteractionFragmentCreateHandler {

	public InteractionUseCreationHandler(final EditPolicy hostPolicy) {
		super(hostPolicy);
	}

	@SuppressWarnings("unchecked")
	public Command[] getCreateCommands(final CreateRequest request) {
		
		boolean isInitialised = initialize(request);
		if (! isInitialised) {
			Debug.log(new Exception("Command initialization error."));
			return null;
		}
		
		// SetBoundsHintCommand [SeDi]
		// Registers a Bounds hint that can be used when the
		// SeDi GraphcialElement is created.
		// CreateInteractionUseCommand [UML]
		// Creates the UML element and adds it to the UML interaction
		// When the command is executed, a GraphicalElement will be
		// created in response to the notification. This creation will
		// consume the Bounds hint set previously
		// AnalyseCoveredCommand [SeDi]
		// Analyses the diagram and detects any lifelines covering the
		// new fragment. The result is input to the next command:
		// CoverCommand [UML]
		// Sets the fragment's Covered attribute.


		final CreateInteractionUseCommand createCmd = new CreateInteractionUseCommand(coveredLifelines, interaction);

		final CreateGraphicalElementCommand createGECmd = new CreateGraphicalElementCommand();
		createGECmd.setAnyEditPart(interactionEP);
		createGECmd.setParentGE(interactionNode);
					
		final AnalyseCoveredCommand analyseCoveredCmd = new AnalyseCoveredCommand();
		analyseCoveredCmd.setEditPart(interactionEP);

		final CoverCommand coverCmd = new CoverCommand();

		final SelectPartCommand selectCmd = new SelectPartCommand();
		selectCmd.setAnyEditPart(interactionEP.getRoot());
		selectCmd.setSelectForEdit(true);

		final Command glueGE = new Command() {
			@Override
			public void execute() {
				createGECmd.setElement(createCmd.getInteractionUse());
			}
		};

		final Command glue1 = new Command() {
			@Override
			public void execute() {
				InteractionFragment interactionUse = createCmd.getInteractionUse();
				coverCmd.setFragment(interactionUse);
				GraphNode interactionUseView = 
					ModelUtil.getGraphNode(interactionUse, diagram);
				analyseCoveredCmd.setFragmentView(interactionUseView);
			}
		};

		final Command glue2 = new Command() {
			@Override
			public void execute() {
				coverCmd.setFragment(createCmd.getInteractionUse());
				coverCmd.setCovered(analyseCoveredCmd.getCovered());
				selectCmd.setTarget(createCmd.getInteractionUse());
			}
		};

		return new Command[] { 
				createCmd, glueGE, 
				createGECmd, glue1, 
				analyseCoveredCmd, glue2, 
				coverCmd, 
				selectCmd };
	}
}
